//地图
(function() {

    function Map(width, height) {
        this.width = width || 600;
        this.height = height || 600;
    }
    window.Map = Map;
    Map.prototype.draw = function() {

    }

}());

let mapObj = new Map();
let dvArr=[];
//格子
(function() {
    let map = document.querySelector("#map");

    function Lattice(width, height) {
        this.width = width || 40;
        this.height = height || 40;
        this.piece = new Piece();
        this.that=this;
        //格子的位置？
    }
    window.Lattice = Lattice;
    Lattice.prototype.draw = function() {
        let count = (mapObj.width / this.width) * (mapObj.height / this.height);
        for (let i = 0; i < (mapObj.width / this.width); i++) {
            for(let j=0;j<(mapObj.height / this.height);j++){
                let dv = document.createElement("div");
                dv.setAttribute("index",[i,j]);
                dv.style.width = this.width + "px";
                dv.style.height = this.height + "px";
                dv.style.border = "1px solid #333333";
                dv.addEventListener("click", this.piece.draw)
                map.append(dv);
            }

        }
    }

}());


//棋子
(function() {
    window.blackPiece=[];
    window.whitePiece=[];
    function Piece() {
    }
    window.Piece = Piece;
    let piece=new Piece();
    Piece.prototype.black="img/timg.jpg";
    Piece.prototype.white="img/bai.jpg";
    //画在地图上
    Piece.prototype.draw = function(e) {
        //获得坐标
        // let x=Math.floor(e.clientX/40);
        // let y=Math.floor(e.clientY/40);
        if(game.player=="black"){
            if(!this.style.backgroundImage){

                this.style.backgroundImage="url("+piece.black+")";
                blackPiece.push(this.getAttribute("index").split(","));
                game.player="white";
            }
        }else{
            if(!this.style.backgroundImage) {
                this.style.backgroundImage = "url(" + piece.white + ")";
                whitePiece.push(this.getAttribute("index").split(","));
                game.player = "black";
            }
        }

        game.judgeGame();


    }
}());






//棋手
(function() {
    function ChessPlayer(name) {
        this.name = name;
        this.piece=new Piece();
    }

    window.ChessPlayer = ChessPlayer;
    ChessPlayer.prototype.play = function() {

    }
}());



//Game
(function() {
    function Game() {
        this.map = new Map();//地图
        this.lattice=new Lattice();
        this.blacker = new ChessPlayer();//黑棋手
        this.Whiteer=new ChessPlayer();//白棋手
        // this.piece=new Piece();

        //正在下棋玩家
        this.player=null;
    }

    window.Game = Game;
    let game=new Game();

    //初始化游戏
    Game.prototype.init = function() {
        this.lattice.draw();
        this.player="black";
    }

    //开始游戏
    Game.prototype.play=function () {
        this.init();

    }

    Game.prototype.judgeGame=function () {
        if(blackPiece.length>4||whitePiece.length>4){
            let player=this.player=="black"?whitePiece:blackPiece;
            let playName=this.player=="black"?"白棋":"黑棋";
            //获得是黑还是白在下棋
            //遍历该色棋是否达到赢的规则
            let flag=0;
            let rowArr=[],colArr=[];
            for (let i = 0; i < player.length; i++) {
                //连续横坐标&&纵不变
                rowArr.push(player[i][0]);
                colArr.push(player[i][1]);

            }

            //排序
            // rowArr.sort(function(a, b){return  a - b});
            // colArr.sort(function(a, b){return  a - b});


           // 连续横坐标&&连续纵坐标
















        }
    }

}());